#include "os.h"
#include "uinit.h"
#include <signal.h>
#include <stdlib.h>

static void
sighandler(int signal)
{
    /* see signal(7) */
    if (
        /* SIGCHLD/SIGCLD*/
        17 == signal ||
        18 == signal ||

        /* SIGURG */
        16 == signal ||
        21 == signal ||
        23 == signal ||

        /* SIGWINCH */
        20 == signal ||
        28 == signal ||

        /* SIGCONT */
        19 == signal ||
        25 == signal ||

        /* SIGSTOP */
        /* SIGTSTP */
        24 == signal ||

        /* SIGTTIN */
        26 == signal ||

        /* SIGTTOU */
        22 == signal ||
        27 == signal)
    {
        return;
    }

    exit(1);
}

namespace log4x
{
uinit::uinit()
{
    for (int i = 0; i < 32; i ++)
    {
        if (
            /* Ignore the signals those are ignored by default */
            SIGCHLD  == i ||
            /* SIGCLD   == i || */
            SIGWINCH == i ||
            SIGURG   == i ||
#ifdef _DEBUG
            /* Ignore the signals those would caught by gdb, 6, 11 */
            SIGSEGV  == i ||
            SIGABRT  == i ||
#endif
            /* Ignore the signals those are stop by default */
            SIGCHLD  == i ||
            SIGCONT  == i ||
            SIGSTOP  == i ||
            SIGTSTP  == i ||
            SIGTTIN  == i ||
            SIGTTOU  == i)
        {
            continue;
        }

        signal(i, sighandler);

        if (i == SIGPIPE)
        {
            signal(SIGPIPE, SIG_IGN);
        }
    }
}

uinit::~uinit()
{}
}
